package com.demo.salary.transactionimplementation;

import com.demo.salary.payrolldomain.Employee;
import com.demo.salary.payrolldomain.PaymentClassification;
import com.demo.salary.payrolldatabase.PayrollDatabase;
import com.demo.salary.transactionapplication.Transaction;
import com.demo.salary.payrollimplementation.HourlyClassification;
import com.demo.salary.payrollimplementation.TimeCard;

import java.time.LocalDate;

public class TimeCardTransaction implements Transaction {
    LocalDate date;
    int hours;
    int empId;

    public TimeCardTransaction(LocalDate date, int hours, int empId) {
        this.date = date;
        this.hours = hours;
        this.empId = empId;
    }

    @Override
    public void execute() {
        Employee employee = PayrollDatabase.getEmployee(empId);
        if (employee != null) {
            PaymentClassification pc = PayrollDatabase.getEmployee(empId).getClassification();

            if (!(pc instanceof HourlyClassification)) {
                throw new RuntimeException("该员工不是钟点工");
            }
            HourlyClassification hc = (HourlyClassification) pc;
            hc.addTimeCard(new TimeCard(date, hours));

        } else {
            throw new RuntimeException("没有该员工");
        }


    }
}
